﻿<template>
    <div>
        <el-radio-group v-model="myValue"
                        :disabled="disabled"
                        @change="change">
            <el-radio-button v-for="item,index in myOptions"
                             :key="index"
                             :label="item[props.value]">{{item[props.label]}}</el-radio-button>
        </el-radio-group>
    </div>
</template>

<script>
export default {
  data () {
    return {
      myValue: ''
    }
  },
  props: {
    // 单选值
    value: [String, Number, Boolean],
    // 选项列表
    options: {
      type: [Array, Function, Object],
      required: true
    },
    // 选项属性定义
    props: {
      type: Object,
      default () {
        return {
          label: 'label',
          value: 'value'
        }
      }
    },
    disabled: {
      type: Boolean,
      default () {
        return false
      }
    },
    objSource: {
      type: [Object]
    }
  },
  watch: {
    value: {
      handler (newVal) {
        this.myValue = newVal
      },
      immediate: true
    },
    myValue (newVal) {
      this.$emit('input', newVal)
    }
  },
  computed: {
    myOptions () {
      var objSource = this.objSource || this.options

      return this.$core.convertOptions({
        options: this.options,
        props: this.props,
        objSource
      })
    }
  },
  methods: {
    change (newVal) {
      this.$emit('change', newVal)
    }
  }
}
</script>
